package durst.utils;

import java.io.File;
import java.sql.SQLException;

import durst.data.Account;
import durst.data.Event;
import durst.data.FileAccess;
import durst.data.dao.DaoFactory;
import durst.data.dao.impl.FileAccessDao;

public class BucketUtil {
	private static String dir = ConfigParser.getProp("durst.bucket.path");

	public static File createFile(Account account, Event event, String fileName)
			throws SQLException {
		FileAccess access = new FileAccess(account, event, fileName);
		DaoFactory.getInstance().createDao(FileAccess.class).create(access);

		return getFile(account, event, fileName);
	}

	public static File getFile(Account acc, Event event, String fileName) {
		return new File(fileName(acc, event, fileName));
	}

	@SuppressWarnings("unchecked")
	public static void deleteFile(Account account, Event event, String filename)
			throws SQLException {
		FileAccessDao<FileAccess, Integer> dao = (FileAccessDao<FileAccess, Integer>) DaoFactory
				.getInstance().createDao(FileAccess.class);
		dao.delete(dao.findByMetadata(account, filename, event));

		getFile(account, event, filename).delete();
	}

	public static String fileName(Account account, Event event, String filename) {
		return dir + account.getId() + "_" + event.getId() + "_" + filename;
	}
}
